<?php

use Zend\Json\Json;

$menuItem = $this->menuItem;
$menuId = $menuItem->id;

$menuAsKendoDataSourceArr = $menuItem->getMenuAsKendoDataSource();
$menuAsKendoDataSourceJson = Json::encode($menuAsKendoDataSourceArr);


?>

<script type="text/javascript">

    var menuAsKendoDataSourceJson = <?php echo $menuAsKendoDataSourceJson?>;
    if(treeview)
    treeview.destroy();
    var treeview = $("#menu_tree_ctn_<?php echo $menuId ?>").kendoTreeView({
        dataSource: menuAsKendoDataSourceJson,
        change: onSelectMenu
    }).data("kendoTreeView");


    treeview.expand(".k-item");
    treeview.select(".k-first");
    function onSelectMenu(e){
        selectedNode = getTreeSelectedNode(treeview)
        data = treeview.dataItem(selectedNode);

        $.post('<?php echo '/admin/menu/get-menu-item-parent'  ?>',
                {id:data.id}
                ,function(result){
                    if (result.success){
                        if(result.upable)
                            $('#btnUpMenu_<?php echo $menuId ?>').show();
                        else
                            $('#btnUpMenu_<?php echo $menuId ?>').hide();
                        if(result.downable)
                            $('#btnDownMenu_<?php echo $menuId ?>').show();
                        else
                            $('#btnDownMenu_<?php echo $menuId ?>').hide();

                        $('#error_ctn').html('');

                    }else{
                        $('#error_ctn').removeClass('blue_text').html(result.msg);
                        alert(result.msg);
                    }
                },'json');


        $('.none_menu_info_ctn').hide();
        $('.menu_info_ctn').show();

        header = 'Menu item : '+ data.label;
        if(data.modifiable == 0)
            header = header+'. Không thể sửa hoặc xóa.';
        $('.header_name').html(header);

        $('#txtMenuItemName').val(data.label);
        $('#txtMenuItemId').val(data.id);
        $('#txtMenuUrl').val(data.my_url);
        cboMenuTarget = $("#cboMenuTarget").data("kendoComboBox");
        cboMenuTarget.value(data.target);
        $('#parameterChooserBox').html('');
        decorateModifiableNode(data);

    }

    function decorateModifiableNode(data){
        if(data.modifiable == 0){
            $('#txtMenuUrl').attr('disabled', true);
            $('#txtMenuItemName').attr('disabled', true);
            cboMenuTarget = $("#cboMenuTarget").data("kendoComboBox");
            cboMenuTarget.enable(false);
            $('#btnChoosePage').hide();
            $('#btnSave').hide();

        }else{
            $('#txtMenuUrl').removeAttr('disabled');
            $('#txtMenuItemName').removeAttr('disabled');
            cboMenuTarget = $("#cboMenuTarget").data("kendoComboBox");
            cboMenuTarget.enable(true);
            $('#btnChoosePage').show();
            $('#btnSave').show();
        }

    }

    function getTreeSelectedNode(treeview){
        selectedNode = treeview.select();
        if (selectedNode.length == 0) {
            treeview.select(".k-item:first");
            selectedNode = treeview.select();
        }
        return selectedNode;
    }

</script>